package org.uim.cricket.afetcher.args.impl;

import org.uim.cricket.afetcher.FetcherProperties;
import org.uim.cricket.afetcher.args.ArgumentGeneratable;

public class DefaultArgumentGenerator implements ArgumentGeneratable {

	@Override
	public String generateArguments(FetcherProperties prop) {
		StringBuilder args = new StringBuilder();
		
		String runAnalyze = prop.getProperty("run.analyze");
		boolean ranal = false;
		if (runAnalyze.equalsIgnoreCase("yes") || runAnalyze.equalsIgnoreCase("true")) {
			args.append("-a ");
			ranal = true;
		}
		
		String parserType = prop.getProperty("parser.type");
		args.append("-type:").append(parserType).append(" ");
		
		String srcFile = prop.getProperty("parser.src.file");
		args.append("-f \"").append(srcFile).append("\" ");
		
		String parserDestDir = prop.getProperty("parser.dest.dir");
		if (!parserDestDir.endsWith("/")) {
			parserDestDir += "/";
		}
		parserDestDir += this.captureFileName(srcFile) + ".xml";
		args.append("\"").append(parserDestDir).append("\" ");
		
		if (ranal) {
			String tmp = prop.getProperty("analyze.type");
			args.append("-aexec:").append(tmp).append(" ");
			
			tmp = prop.getProperty("analyze.dest.file.ext");
			if (tmp != null) {
				args.append("-arep:").append(tmp);
			}
		}
		
		return args.toString();
	}

	private String captureFileName(String src) {
		int spos = src.lastIndexOf("\\");
		int epos = src.lastIndexOf(".");
		if (spos < epos && spos >= 0) {
			return src.substring(spos+1, epos);
		}
		return src;
	}
}
